package demo.practice.tree.depth;

import demo.practice.TreeNode;

public class Offer55_2 {


    public static void main(String[] args) {
        Integer deep = 1000;
        Offer55_2 obj = new Offer55_2();
        obj.add(deep);
        System.out.println(deep);
    }

    public void add(Integer deep) {
        deep = deep + 1;
    }


    public boolean isBalanced(TreeNode root) {
        return isBalanced(root, new int[]{0});
    }

    public boolean isBalanced(TreeNode root, int[] deep) {
        if (root == null) {
            deep[0] = 0;
            return true;
        }
        int[] deepL = new int[]{0}, deepR = new int[]{0};

        if (!isBalanced(root.left, deepL) || !isBalanced(root.right, deepR)) {
            return false;
        }

        if (Math.abs(deepL[0] - deepR[0]) > 1) {
            return false;
        }
        deep[0] = Math.max(deepL[0], deepR[0]) + 1;
        return true;
    }
}
